/*
 * @Author: SakumyZ
 * @Date: 2019-04-28 19:40:27
 * @Description:  该控制器负责请求处理
 * @Last Modified by: SakumyZ
 * @Last Modified time: 2019-04-29 10:48:57
 */

const formidable = require('formidable');
const dbController = require('./dbController');

const loginHandle = function(req, res) {
  console.log('表单提交完成');
  const form = new formidable.IncomingForm();
  form.parse(req, (err, fields) => {
    if (err) return '-1';
    let userName = fields.userName;
    let password = fields.password;
    console.log(fields);
    // 表单登录验证
    dbController.findUser(userName, password, flag => {
      console.log(flag);
      // 验证成功，向客户端发送状态码
      if (flag) {
        res.send('1');
      } else {
        res.send('-1');
      }
    });
  });
};

const registeredHandle = function(req, res) {
  console.log('表单提交完成');
  const form = new formidable.IncomingForm();
  form.parse(req, (err, fields) => {
    if (err) return '-1';
    let userName = fields.userName;
    let password = fields.password;
    console.log(fields);
    // 表单登录验证
    dbController.insert(userName, password, flag => {
      // 插入数据库成功，向客户端发送状态码
      console.log('由requestController.js第44行打印' + flag);
      if (flag) {
        res.send('1');
      } else {
        res.send('-1');
      }
    });
  });
};
exports.loginHandle = loginHandle;
exports.registeredHandle = registeredHandle;
